﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.OleDb;

namespace DAO
{
    public class PhongDAO: AbtractDAO
    {
        public static List<PhongDTO> LayDSPhong()
        {
            OleDbConnection ketNoi = null;
            List<PhongDTO> dsPhong = new List<PhongDTO>();
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT MaPhong,TenPhong,P.MaLoaiPhong,TenLoaiPhong,GiaTien,TinhTrang,GhiChu FROM PHONG P, LOAIPHONG LP WHERE P.MaLoaiPhong=LP.MaLoaiPhong";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbDataReader boLoc = lenh.ExecuteReader();

                while (boLoc.Read())
                {
                    PhongDTO Phong = new PhongDTO();
                    Phong.MaPhong = boLoc.GetInt32(0);
                    if (!boLoc.IsDBNull(1))
                    {
                        Phong.TenPhong = boLoc.GetString(1);
                    }
                    LoaiPhongDTO lp = new LoaiPhongDTO();
                    lp.MaLoaiPhong = boLoc.GetInt32(2);
                    if (!boLoc.IsDBNull(3))
                    {
                        lp.TenLoaiPhong = boLoc.GetString(3);
                    }
                    Phong.LoaiPhong = lp;
                    
                    if (!boLoc.IsDBNull(4))
                    {
                        lp.DonGia = boLoc.GetInt32(4);
                    }
                    Phong.DonGia = lp;

                    if (!boLoc.IsDBNull(5))
                    {
                        Phong.TinhTrang = boLoc.GetInt32(5);
                    }

                    if (!boLoc.IsDBNull(6))
                    {
                        Phong.GhiChu = boLoc.GetString(6);
                    }

                    dsPhong.Add(Phong);
                }
            }
            catch (Exception ex)
            {
                dsPhong = new List<PhongDTO>();
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return dsPhong;
        }

        public static List<PhongDTO> LayDSPhongTheoLoaiPhong(string loaiPhong)
        {
            OleDbConnection ketNoi = null;
            List<PhongDTO> dsPhong = new List<PhongDTO>();
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT MaPhong,TenPhong,P.MaLoaiPhong,TenLoaiPhong,GiaTien,TinhTrang FROM PHONG P, LOAIPHONG LP WHERE P.MaLoaiPhong=LP.MaLoaiPhong AND TenLoaiPhong=@TenLoaiPhong";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo2 = new OleDbParameter("@TenLoaiPhong", OleDbType.Char);
                thamSo2.Value = loaiPhong;

                lenh.Parameters.Add(thamSo2);

                OleDbDataReader boLoc = lenh.ExecuteReader();

                while (boLoc.Read())
                {
                    PhongDTO Phong = new PhongDTO();
                    Phong.MaPhong = boLoc.GetInt32(0);
                    if (!boLoc.IsDBNull(1))
                    {
                        Phong.TenPhong = boLoc.GetString(1);
                    }
                    LoaiPhongDTO lp = new LoaiPhongDTO();
                    lp.MaLoaiPhong = boLoc.GetInt32(2);
                    if (!boLoc.IsDBNull(3))
                    {
                        lp.TenLoaiPhong = boLoc.GetString(3);
                    }
                    Phong.LoaiPhong = lp;

                    if (!boLoc.IsDBNull(4))
                    {
                        lp.DonGia = boLoc.GetInt32(4);
                    }
                    Phong.DonGia = lp;

                    if (!boLoc.IsDBNull(5))
                    {
                        Phong.TinhTrang = boLoc.GetInt32(5);
                    }

                    dsPhong.Add(Phong);
                }
            }
            catch (Exception ex)
            {
                dsPhong = new List<PhongDTO>();
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return dsPhong;
        }
        public static PhongDTO LayDSPhongTheoMaPhong(string maphong)
        {
            OleDbConnection ketNoi = null;
            PhongDTO Phong = new PhongDTO();
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT MaPhong,TenPhong,P.MaLoaiPhong,TenLoaiPhong,GiaTien,TinhTrang FROM PHONG P, LOAIPHONG LP WHERE P.MaLoaiPhong=LP.MaLoaiPhong AND P.MaPhong=@MaPhongPhong";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo2 = new OleDbParameter("@MaPhong", OleDbType.Integer);
                thamSo2.Value = Convert.ToInt32(maphong);

                lenh.Parameters.Add(thamSo2);

                OleDbDataReader boLoc = lenh.ExecuteReader();
               
                if (boLoc.Read())
                {
                    
                    Phong.MaPhong = boLoc.GetInt32(0);
                    if (!boLoc.IsDBNull(1))
                    {
                        Phong.TenPhong = boLoc.GetString(1);
                    }
                    LoaiPhongDTO lp = new LoaiPhongDTO();
                    lp.MaLoaiPhong = boLoc.GetInt32(2);
                    if (!boLoc.IsDBNull(3))
                    {
                        lp.TenLoaiPhong = boLoc.GetString(3);
                    }
                    Phong.LoaiPhong = lp;

                    if (!boLoc.IsDBNull(4))
                    {
                        lp.DonGia = boLoc.GetInt32(4);
                    }
                    Phong.DonGia = lp;

                    if (!boLoc.IsDBNull(5))
                    {
                        Phong.TinhTrang = boLoc.GetInt32(5);
                    }

                    
                }
            }
            catch (Exception ex)
            {
                 Phong = new PhongDTO();
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return Phong;
        }
        public static List<PhongDTO> LayDSPhongTheoMaKH(string maKH)
        {
            OleDbConnection ketNoi = null;
            List<PhongDTO> dsPhong = new List<PhongDTO>();
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT P.MaPhong, P.TenPhong, P.MaLoaiPhong,LP.TenLoaiPhong,LP.GiaTien,P.TinhTrang,P.GhiChu from LOAIPHONG LP,PHONG P,PHIEUTHUE PT,KHACHHANG KH,CHITIETPHIEUTHUE CTPT where KH.MaKhachHang = CTPT.MaKhachHang and CTPT.MaPhieuThue = PT.MaPhieuThue and PT.MaPhong = P.MaPhong and P.MaLoaiPhong = LP.MaLoaiPhong and KH.MaKhachHang = @MaKH ";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo = new OleDbParameter("@MaKH", OleDbType.Integer);
                thamSo.Value = Convert.ToInt32(maKH);
                lenh.Parameters.Add(thamSo);

                OleDbDataReader boLoc = lenh.ExecuteReader();

                while (boLoc.Read())
                {
                    PhongDTO Phong = new PhongDTO();
                    Phong.MaPhong = boLoc.GetInt32(0);
                    if (!boLoc.IsDBNull(1))
                    {
                        Phong.TenPhong = boLoc.GetString(1);
                    }
                    LoaiPhongDTO lp = new LoaiPhongDTO();
                    lp.MaLoaiPhong = boLoc.GetInt32(2);
                    if (!boLoc.IsDBNull(3))
                    {
                        lp.TenLoaiPhong = boLoc.GetString(3);
                    }
                    

                    if (!boLoc.IsDBNull(4))
                    {
                        lp.DonGia = boLoc.GetInt32(4);
                    }
                    Phong.LoaiPhong= lp;

                    if (!boLoc.IsDBNull(5))
                    {
                        Phong.TinhTrang = boLoc.GetInt32(5);
                    }
                    if (!boLoc.IsDBNull(6))
                    {
                        Phong.GhiChu = boLoc.GetString(6);
                    }

                    dsPhong.Add(Phong);
                }
            }
            catch (Exception ex)
            {
                dsPhong = new List<PhongDTO>();
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return dsPhong;
        }
        public static String layNgayBDThue(string maKH,string maphong)
        {
            OleDbConnection ketNoi = null;
            string ngay="";
            try
            {

                ketNoi = KetNoi();
                string chuoiLenh = "SELECT PT.NgayBatDauThue from LOAIPHONG LP,PHONG P,PHIEUTHUE PT,KHACHHANG KH,CHITIETPHIEUTHUE CTPT where KH.MaKhachHang = CTPT.MaKhachHang and CTPT.MaPhieuThue = PT.MaPhieuThue and PT.MaPhong = P.MaPhong and P.MaLoaiPhong = LP.MaLoaiPhong and KH.MaKhachHang = @MaKH and P.MaPhong = @MaPhong";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo = new OleDbParameter("@MaKH", OleDbType.Char);
                thamSo.Value = maKH;
                lenh.Parameters.Add(thamSo);
                thamSo = new OleDbParameter("@MaPhong", OleDbType.Integer);
                thamSo.Value = Convert.ToInt32(maphong);
                lenh.Parameters.Add(thamSo);
                
               
                OleDbDataReader boLoc = lenh.ExecuteReader();
                if (boLoc.Read())
                {
                    ngay = boLoc.GetDateTime(0).ToString();
                }
            }
            catch (Exception ex)
            {
                ngay = "khong xac dinh duoc";
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ngay;
            
        }
        
        public static long layGiaTien(string maKH, string maphong)
        {
            OleDbConnection ketNoi = null;
            long ngay = 0;
            try
            {

                ketNoi = KetNoi();
                string chuoiLenh = "SELECT LP.GiaTien from LOAIPHONG LP,PHONG P,PHIEUTHUE PT,KHACHHANG KH,CHITIETPHIEUTHUE CTPT where KH.MaKhachHang = CTPT.MaKhachHang and CTPT.MaPhieuThue = PT.MaPhieuThue and PT.MaPhong = P.MaPhong and P.MaLoaiPhong = LP.MaLoaiPhong where KH.MaKH = @MaKH and P.MaPhong = @MaPhong";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo = new OleDbParameter("@MaKH", OleDbType.Char);
                thamSo.Value = maKH;
                OleDbParameter thamSo2 = new OleDbParameter("@MaPhong", OleDbType.Char);
                thamSo.Value = maphong;

                lenh.Parameters.Add(thamSo);
                lenh.Parameters.Add(thamSo2);
                OleDbDataReader boLoc = lenh.ExecuteReader();
                if (boLoc.Read())
                {
                    ngay = boLoc.GetInt32(0);
                }
            }
            catch (Exception ex)
            {
                ngay = 0;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ngay;

        }
        public static bool ThemPhong(string tenPhong, long maLoaiPhong, long tinhTrang, string ghiChu)
        {
            bool ketQua = true;
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "INSERT INTO PHONG(TenPhong,MaLoaiPhong,TinhTrang,GhiChu) VALUES(@TenPhong,@MaLoaiPhong,@TinhTrang,@GhiChu)";

                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo;

                thamSo = new OleDbParameter("@TenPhong", OleDbType.VarChar);
                thamSo.Value = tenPhong;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@MaLoaiPhong", OleDbType.Integer);
                thamSo.Value = maLoaiPhong;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@TinhTrang", OleDbType.Integer);
                thamSo.Value = tinhTrang;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@GhiChu", OleDbType.VarChar);
                thamSo.Value = ghiChu;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketQua = false;
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }

        public static void XoaPhong(long maPhong)
        {
            OleDbConnection ketNoi = null;

            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "DELETE FROM PHONG WHERE MaPhong = @MaPhong";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);
                //OleDbDataReader boDoc = lenh.ExecuteReader();
                OleDbParameter thamSo;
                thamSo = new OleDbParameter("@MaPhong", OleDbType.Integer);
                thamSo.Value = maPhong;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
        }

        public static void UpdatePhong(long maPhong, string tenPhong, long maLoaiPhong, long tinhTrang, string ghiChu)
        {
            OleDbConnection ketNoi = null;

            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "UPDATE PHONG SET TenPhong=@TenPhong,MaLoaiPhong=@MaLoaiPhong,TinhTrang=@TinhTrang,GhiChu=@GhiChu WHERE MaPhong=@MaPhong";
          
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);
                OleDbParameter thamSo;


                thamSo = new OleDbParameter("@TenPhong", OleDbType.VarChar);
                thamSo.Value = tenPhong;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@MaLoaiPhong", OleDbType.Integer);
                thamSo.Value = maLoaiPhong;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@TinhTrang", OleDbType.Integer);
                thamSo.Value = tinhTrang;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@GhiChu", OleDbType.VarChar);
                thamSo.Value = ghiChu;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@MaPhong", OleDbType.Integer);
                thamSo.Value = maPhong;
                lenh.Parameters.Add(thamSo);


                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
        }
    }
}
